package cn.edu.dlut.ssdut.foodCredit2012.service;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import cn.edu.dlut.ssdut.foodCredit2012.model.UploadImage;
import cn.edu.dlut.ssdut.foodCredit2012.util.HibernateUtil;

public class UploadImageService {
	public void add(UploadImage image) {
		Session session = HibernateUtil.getFileSessionFactory().getCurrentSession();
		session.beginTransaction();
		session.save(image);
		session.getTransaction().commit();
	}

	public void delete(UploadImage image) {
		Session session = HibernateUtil.getFileSessionFactory().getCurrentSession();
		session.beginTransaction();
		session.delete(image);
		session.getTransaction().commit();
	}
	
	public void upload(UploadImage image) {
		Session session = HibernateUtil.getFileSessionFactory().getCurrentSession();
		session.beginTransaction();
		session.delete(image);
		session.getTransaction().commit();
	}

	public List<UploadImage> loadFiveImage() {
		Session session = HibernateUtil.getFileSessionFactory().getCurrentSession();
		session.beginTransaction();
		Query query = session.createQuery("from UploadImage");
		query.setFirstResult(0);
		query.setMaxResults(5);
		List<UploadImage> list = (List<UploadImage>) query.list();
		session.getTransaction().commit();
		return list;
	}
}
